﻿using SWBetongXaydung.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SWBetongXaydung.BLL
{
    class PhanCongLaiXeBLL
    {
        public static List<PhanCongLaiXe> GetList(int thang = 0, int nam = 0, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
            {
                db = dbContext;
            }
            else
                db = DBContextHelper.GetContext();
            if (thang == 0)
            {
                var list = (from e in db.PhanCongLaiXeDbSet
                            orderby e.TaiXe.Ten ascending
                            select e).ToList();
                if (dbContext == null)
                    db.Dispose();
                return list;
            }
            else
            {
                var list = (from e in db.PhanCongLaiXeDbSet
                            where e.NgayNhanXe.Month == thang && e.NgayNhanXe.Year == nam
                            orderby e.TaiXe.Ten ascending
                            select e);
                if (dbContext == null)
                    db.Dispose();
                return list.ToList();
            }
        }

        public static PhanCongLaiXe Get(string soXe, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
                db = dbContext;
            else
                db = DBContextHelper.GetContext();
            var list = (from e in db.PhanCongLaiXeDbSet
                        where e.SoXe == soXe && !e.NgayTraXe.HasValue
                        orderby e.NgayNhanXe descending
                        select e).ToList();
            if (list != null && list.Count > 0)
            {
                if (dbContext == null)
                    db.Dispose();
                return list[0];
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return null;
            }
        }
        public static PhanCongLaiXe Get(int IDTaiXe, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
                db = dbContext;
            else
                db = DBContextHelper.GetContext();
            var list = (from e in db.PhanCongLaiXeDbSet
                        where e.IDTaiXe == IDTaiXe
                        orderby e.NgayNhanXe descending
                        select e).ToList();
            if (list != null && list.Count > 0)
            {
                if (dbContext == null)
                    db.Dispose();
                return list[0];
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return null;
            }
        }

        public static CODE_RESULT_RETURN Add(PhanCongLaiXe phanCongLaiXe, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
            {
                db = dbContext;
            }
            else
                db = DBContextHelper.GetContext();

            var obj = (from e in db.PhanCongLaiXeDbSet
                       where e.IDTaiXe == phanCongLaiXe.IDTaiXe && e.SoXe == phanCongLaiXe.SoXe && e.NgayNhanXe == phanCongLaiXe.NgayNhanXe
                       select e).SingleOrDefault();
            if (obj != null)
            {
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.MaTrung;
            }
            else
            {
                var objs = (from e in db.PhanCongLaiXeDbSet
                            where e.SoXe == phanCongLaiXe.SoXe
                            orderby e.SoXe descending
                            select e).ToList();
                if (objs != null && objs.Count > 0)
                {
                    if (objs[0].NgayTraXe.HasValue)
                    {
                        db.PhanCongLaiXeDbSet.Add(phanCongLaiXe);
                        db.SaveChanges();
                        if (dbContext == null)
                            db.Dispose();
                        return CODE_RESULT_RETURN.ThanhCong;
                    }
                    else
                    {
                        if (dbContext == null)
                            db.Dispose();
                        return CODE_RESULT_RETURN.TenTrung;
                    }
                }
                else
                {
                    db.PhanCongLaiXeDbSet.Add(phanCongLaiXe);
                    db.SaveChanges();
                    if (dbContext == null)
                        db.Dispose();
                    return CODE_RESULT_RETURN.ThanhCong;
                }
            }
        }

        public static CODE_RESULT_RETURN Update(PhanCongLaiXe phanCongLaiXe, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
            {
                db = dbContext;
            }
            else
                db = DBContextHelper.GetContext();
            var obj = (from e in db.PhanCongLaiXeDbSet
                       where e.IDPhanCong == phanCongLaiXe.IDPhanCong
                       select e).SingleOrDefault();
            if (obj != null)
            {
                obj.SoXe = phanCongLaiXe.SoXe;
                obj.IDTaiXe = phanCongLaiXe.IDTaiXe;
                obj.NgayNhanXe = phanCongLaiXe.NgayNhanXe;
                obj.LuongNhienLieuKhiNhanXe = phanCongLaiXe.LuongNhienLieuKhiNhanXe;
                obj.NgayTraXe = phanCongLaiXe.NgayTraXe;
                obj.LuongNhienLieuKhiTraXe = phanCongLaiXe.LuongNhienLieuKhiTraXe;
                db.SaveChanges();
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThanhCong;
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThatBai;
            }
        }

        public static CODE_RESULT_RETURN Delete(int IDPhanCong, BETONGXAYDUNGEntities dbContext = null)
        {
            BETONGXAYDUNGEntities db = null;
            if (dbContext != null)
                db = dbContext;
            else
                db = DBContextHelper.GetContext();
            var obj = db.PhanCongLaiXeDbSet.Where(e => e.IDPhanCong == IDPhanCong).SingleOrDefault();
            if (obj != null)
            {
                db.PhanCongLaiXeDbSet.Remove(obj);
                db.SaveChanges();
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThanhCong;
            }
            else
            {
                if (dbContext == null)
                    db.Dispose();
                return CODE_RESULT_RETURN.ThatBai;
            }
        }
    }
}
